//
//  TCLiveUSERCell.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/3/2.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class TCLiveUSERCell: TableRowView {
    
    var info:UserInfo?
    var isJoin:Bool = false
    
    /// 选择
    var highViews:[ZHButton] = []
    
    var highAction:OneHightMune?
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
    }
    
    convenience init(info:UserInfo,isJoin:Bool,identifier:String) {
        self.init()
        self.info = info
        self.isJoin = isJoin
        self.identifier = NSUserInterfaceItemIdentifier.init(identifier)
        self.setupUI()
        self.loadData()
    }
    
    
    func loadData()  {
        guard let info = self.info else {
            return
        }
        if info.userId?.isCurret() == true  {
            self.isMute = MTNot.getLocalMute()
        }else {
            self.isMute = info.isMute
        }
        self.isVdOpen = info.isVdOpen
        self.handImg.isHidden = !info.isHandUp
        self.isAdc = info.isAdc
        if info.userId?.isFocusId() == true {
            self.focuImg.isHidden = false
        }
        
        let infoList = TCGirdRender.shared().renderer.infoList
        let isok = infoList.contains { (inho) -> Bool in
            return inho.userId == info.userId
        }
        self.isVisible = isok
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    var isHigh:Bool = false {
        didSet {
            let bgColor = isHigh ? color_dark70:color_dark
            self.backgroundColor = bgColor
            if self.highViews.count > 0 {
                self.contentView.isHidden = isHigh
                self.contentHighView.isHidden = !isHigh
            }
        }
    }
    
    var isAdc:Bool = false {
        didSet {
            self.audioImg.isHidden  = self.isAdc
            self.videoImg.isHidden  = self.isAdc
        }
    }
    
    var isMute:Bool = false {
        didSet {
            let image = isMute ? "toolbar_unmute_voip_press":"toolbar_mute_voip_normal"
            self.audioImg.image = NSImage.image(name:image)
        }
    }
    
    var isVdOpen:Bool = false {
        didSet {
            let image = isVdOpen ? "toolbar_stop_video_normal":"toolbar_start_video_normal"
            self.videoImg.image = NSImage.image(name:image)
        }
    }
    
    var isVisible:Bool = false {
        didSet{
            self.videoImg.isHidden = !isVisible
        }
    }
    
    lazy var mouseTrackingArea: NSTrackingArea = {
        let area = NSTrackingArea.init(rect: self.bounds, options: [NSTrackingArea.Options.mouseEnteredAndExited,NSTrackingArea.Options.activeAlways], owner: self, userInfo: nil)
        return area
    }()
    
    
    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
        self.addTrackingArea(self.mouseTrackingArea)
        self.becomeFirstResponder()
    }
    
    lazy var lineView: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(80).cgColor
        view.isHidden = true
        return view
    }()
    
    func setupUI()  {
        self.wantsLayer = true
        self.backgroundColor = color_dark70
        
        self.addSubview(avatarImg)
        avatarImg.snp.makeConstraints { (make) in
            make.width.height.equalTo(30)
            make.centerY.equalToSuperview()
            make.left.equalTo(10)
        }
        
        self.addSubview(title)
        title.snp.makeConstraints { (make) in
            make.left.equalTo(45)
            make.centerY.equalToSuperview()
            make.height.equalTo(20)
            make.width.lessThanOrEqualTo(100)
        }
        
        self.addSubview(identityLab)
        identityLab.snp.makeConstraints { (make) in
            make.left.equalTo(title.snp.right).offset(5)
            make.centerY.equalToSuperview()
            make.height.equalTo(20)
        }
        
        self.addSubview(contentView)
        contentView.snp.makeConstraints { (make) in
            make.left.equalTo(identityLab.snp.right)
            make.right.equalToSuperview()
            make.height.centerY.equalToSuperview()
        }
        
        self.addSubview(contentHighView)
        contentHighView.snp.makeConstraints { (make) in
            make.left.equalTo(10)
            make.right.equalToSuperview()
            make.height.centerY.equalToSuperview()
        }
        
        contentView.addSubview(videoImg)
        videoImg.snp.makeConstraints { (make) in
            make.width.height.equalTo(20)
            make.right.equalTo(-10)
            make.centerY.equalToSuperview()
        }
        
        contentView.addSubview(audioImg)
        audioImg.snp.makeConstraints { (make) in
            make.width.height.equalTo(20)
            make.right.equalTo(-40)
            make.centerY.equalToSuperview()
        }
        
        contentView.addSubview(handImg)
        handImg.snp.makeConstraints { (make) in
            make.width.height.equalTo(20)
            make.right.equalTo(-70)
            make.centerY.equalToSuperview()
        }
        
        contentView.addSubview(focuImg)
        focuImg.snp.makeConstraints { (make) in
            make.width.height.equalTo(20)
            make.right.equalTo(-98)
            make.centerY.equalToSuperview()
        }
        
        self.highViews = self.initContentHighViews(inView: contentHighView)
        
        self.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.width.centerX.equalToSuperview()
            make.bottom.equalTo(0)
            make.height.equalTo(0.6)
        }
        self.isHigh = false
    }
    
    
    func initContentHighViews(inView:NSView) -> [ZHButton] {
        let names = TCLiveUSERViewController.bacisTitls(info: self.info!, isjoin: self.isJoin)
        var highViews:[ZHButton] = []
        for (inx,val) in names.enumerated() {
            let btn = ZHButton()
            btn.zh_font = NSFont.systemFont(ofSize: 12)
            btn.zh_title = val
            btn.zh_color = NSColor.white
            btn.zh_backgroundColor = color_main108
            btn.hasBorder = true
            btn.zh_cornerRadius = 3
            btn.target = self
            btn.action = #selector(highBtnAction(_:))
            btn.tag = inx+100
            inView.addSubview(btn)
            highViews.append(btn)
            let btnWidth = val.ga_width(font: 12)+10
            btn.snp.makeConstraints { (make) in
                make.width.equalTo(btnWidth)
                make.height.equalTo(20)
                make.centerY.equalToSuperview()
                if inx == 0 {
                    make.right.equalTo(-20)
                }else {
                   make.right.equalTo(highViews[inx-1].snp.left).offset(-10)
                }
            }
        }
        return highViews
    }
    
    
    /// 设置标题（从右向左）
    /// - Parameter title: 标题组
    func setHightViewTitles(title:[String]) {
        for (inx,val) in highViews.enumerated() {
            val.title = title[inx]
        }
    }
    
    @objc func highBtnAction(_ senber:ZHButton) {
        if highAction != nil {
            self.highAction!(self as TableRowView, senber)
        }
        
    }
    
    
    lazy var focuImg: NSImageView = {
        let image = NSImageView.init(image: NSImage.init(named: NSImage.Name.init("toolbar_focuid_normal"))!)
        image.isHidden = true
        return image
    }()
    
    lazy var avatarImg: KImageView = {
        let image = KImageView.init(frame: NSRect.zero)
        image.layer?.cornerRadius = 5
        image.layer?.masksToBounds = true
        return image
    }()
    
    lazy var title: NSLabel = {
        let lab = NSLabel.label(title: "...", textColor: color_dark160, font: NSFont.systemFont(ofSize: 15))
        lab.cell?.lineBreakMode = .byCharWrapping
        lab.cell?.truncatesLastVisibleLine = true
        lab.sizeToFit()
        return lab
    }()
    
    lazy var identityLab: NSLabel = {
        let lab = NSLabel.label(title: "...", textColor: color_dark160, font: NSFont.systemFont(ofSize: 15))
         lab.sizeToFit()
        return lab
    }()
    
    lazy var videoImg: NSImageView = {
        let image = NSImageView.init(image: NSImage.init(named: NSImage.Name.init("toolbar_stop_video_normal"))!)
        return image
    }()
    
    lazy var audioImg: NSImageView = {
        let image = NSImageView.init(image: NSImage.init(named: NSImage.Name.init("toolbar_mute_voip_normal"))!)
        return image
    }()
    
    lazy var handImg: NSImageView = {
        let image = NSImageView.init(image: NSImage.init(named: NSImage.Name.init("icon_adc_hand"))!)
        image.isHidden = true
        return image
    }()
    
    lazy var contentView: NSView = {
        let view = NSView()
        return view
    }()
    
    lazy var contentHighView: NSView = {
        let view = NSView()
        view.isHidden = false
        return view
    }()
}

extension TCLiveUSERCell {
    override func mouseEntered(with event: NSEvent) {
        self.isHigh = true
    }
    override func mouseExited(with event: NSEvent) {
        self.isHigh = false
    }
}
